import axios from 'axios'

// 1.实例化一个axios对象
let service = axios.create({
    timeout: 10 * 1000,
    baseURL: '',
})
// 2.设置请求拦截器
service.interceptors.request.use(
    (config) => {
        config.headers['Authorization'] = 'Bearer' + ' ' + localStorage.getItem('token');
        return config;
    },
    (error) => {
        return Promise.reject(error)
    }

)
// 3设置响应拦截器
service.interceptors.request.use(
    (res) => {
        return res;
    },
    (error) => {
        if (error.response.status == 401) {
            alert('登录过期，请重新登录')
        } else if (error.response.status == 404) {
            alert('访问路劲有误')
        } else if (error.response.status == 500) {
            alert('服务器内部错误')
        } else {
            alert('其他错误')
        }
        return Promise.reject(error)
    }
)

// 4.导出service
export default service;